/*
 * Copyright 2010 Christian Matzat and others
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package net.matzat.android.hiberdroid.example;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import net.matzat.android.hiberdroid.criteria.Criteria;
import net.matzat.android.hiberdroid.criteria.Restriction;

public class MainActivity extends ListActivity {

    private final static int NEW_ACTION = 1;
    private final static int EDIT_ACTION = 2;
    private final static int DELETE_ACTION = 3;

    final static String CONTACT_ID = "contact_id";

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        fillView();
        registerForContextMenu(getListView());

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

    }

    @Override
    protected void onListItemClick(final ListView l, final View v, final int position, final long id) {
        super.onListItemClick(l, v, position, id);
        try {
            Criteria<Contact> crit = new Criteria<Contact>(Contact.class);
            crit.add(Restriction.idEq(id));
            Contact contact = crit.uniqueResult();
            if (contact != null) {
                Toast.makeText(this, contact.getFullName(), Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, EDIT_ACTION, Menu.NONE, R.string.menu_edit);
        menu.add(0, DELETE_ACTION, Menu.NONE, R.string.menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case DELETE_ACTION:
                Criteria<Contact> crit = new Criteria<Contact>(Contact.class);
                crit.add(Restriction.idEq(info.id));
                Contact contact = crit.uniqueResult();
                HiberDroidExampleApp.getDB().delete(contact);
                Toast.makeText(this, "delete " + info.id + " successful", Toast.LENGTH_LONG).show();
                fillView();
                return true;
            case EDIT_ACTION:
                Intent editIntent = new Intent();
                editIntent.setClass(this, EditActivity.class);
                editIntent.putExtra(CONTACT_ID, info.id);
                startActivity(editIntent);
                return true;
            case NEW_ACTION:
                Intent newIntent = new Intent();
                newIntent.setClass(this, EditActivity.class);
                startActivity(newIntent);
                return true;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        boolean b = super.onCreatePanelMenu(featureId, menu);
        menu.add(0, NEW_ACTION, Menu.NONE, R.string.menu_new);
        return b;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
            case NEW_ACTION:
                Intent newIntent = new Intent();
                newIntent.setClass(this, EditActivity.class);
                startActivity(newIntent);
                return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    private void fillView() {
        try {
            Criteria<Contact> crit = new Criteria<Contact>(Contact.class);
            ContactsAdapter tableAdapter = new ContactsAdapter(this, crit.list());
            setListAdapter(tableAdapter);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        fillView();
    }
}
